package kt.com.bilin.coin.common

import kt.com.bilin.coin.bean.LoginBean
import kt.com.bilin.coin.bean.UserInfoBean
import kt.com.bilin.coin.helper.mohsi.MoshiUtils
import kt.com.bilin.coin.utils.Logger
import kt.com.bilin.coin.utils.SPUtils

object UserDataCache {
    // 用户token
    var token: String = ""
        set(value) {
            SPUtils.put(SP_KEY_TOKEN, value)
            field = value
        }
        get() {
            if (field.isEmpty())
                field = SPUtils.getString(SP_KEY_TOKEN, "")
            return field
        }

    // 用户登录信息
    var loginInfo: LoginBean = LoginBean()
        set(value) {
            SPUtils.put(SP_KEY_LOGIN_INFO, MoshiUtils.toJSONString(value))
            token = value.csrfToken
            userInfo = value.userInfo
            field = value
        }
        get() {
            if (field.csrfToken.isEmpty()) {
                field = MoshiUtils.parseObject(SPUtils.getString(SP_KEY_LOGIN_INFO, "{}"), LoginBean::class.java)!!
            }
            return field
        }

    // 用户基本信息
    var userInfo: UserInfoBean = UserInfoBean()
        set(value) {
            SPUtils.put(SP_KEY_USER_INFO, MoshiUtils.toJSONString(value))
            field = value
        }
        get() {
            if (field.id <= 0) {
                field = MoshiUtils.parseObject(SPUtils.getString(SP_KEY_USER_INFO, "{}"), UserInfoBean::class.java)!!
            }
            return field
        }

    fun clear() {
        loginInfo = LoginBean()
    }
}